// 监听checkbox
let checkboxes = document.querySelectorAll('.checkbox');
// 获取总价
let totalPrice = document.querySelector('.to-price');
//获取全选框
let allBox = document.querySelector('#total-box');
// 获取选中的商品的显示个数
let goods = document.querySelector('.num-2');
let toPrice = 0; //总价
let allgoods = 0; //商品总个数


// 全选按钮点击处理
allBox.addEventListener('change', function () {
    for (var i = 0; i < checkboxes.length - 1; i++) {
        checkboxes[i].checked = allBox.checked;
    }
    updateTotalPrice();
});


// 单选选中
for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener('change', function () {
        var allChecked = true;           //对全选框的影响
        for (var j = 0; j < checkboxes.length - 1; j++) {
            if (!checkboxes[j].checked) {
                allChecked = false;
                break;
            }
        }
        allBox.checked = allChecked;
        updateTotalPrice();
        goods.textContent = calculateGoodsCount();

    });
}

// 计算商品个数
function calculateGoodsCount() {
    allgoods = 0;
    for (var i = 0; i < checkboxes.length - 1; i++) {
        if (checkboxes[i].checked) {
            allgoods++;
        }
    }
    return allgoods;
}



// 计算总价
function updateTotalPrice() {
    toPrice = 0;
    for (var i = 0; i < checkboxes.length - 1; i++) { //id 为closed的不计算价格----交易成功/交易关闭
        if (checkboxes[i].id !== 'closed') {
            if (checkboxes[i].checked && !isNaN(parseInt(checkboxes[i].dataset.price))) {
                var price = parseInt(checkboxes[i].dataset.price);
                toPrice += price;
            }
        }
    }
    totalPrice.textContent = toPrice + '元';
    goods.textContent = calculateGoodsCount();
}


//模块弹窗
var openModalBtn = document.getElementById('openModalBtn');
var closeModalBtn = document.getElementById('closeModalBtn');
var closeModalBtns = document.getElementById('closeModalBtn-1');
var modalContainer = document.getElementById('modalContainer');
var modalContainers = document.getElementById('modalContainer-1');
let cancel = document.querySelector('.cancel')
let payDom = document.querySelector('.pay')
openModalBtn.addEventListener('click', function () {
    modalContainer.style.display = 'block';
});

closeModalBtn.addEventListener('click', function () {
    modalContainer.style.display = 'none';
});
cancel.addEventListener('click', function () {
    modalContainer.style.display = 'none';
})
payDom.addEventListener('click', function () {
    modalContainer.style.display = 'none';
    modalContainers.style.display = 'block'
})
closeModalBtns.addEventListener('click', function () {
    modalContainers.style.display = 'none';
});

//删除某一个商品
var itemsContainer = document.getElementById('items-container');
function deleteItem(event) {
    var itemElement = event.target.closest('.order-item-list');
    itemElement.remove();
}

//删除按钮
var deleteButtons = document.getElementsByClassName('del');
for (var i = 0; i < deleteButtons.length; i++) {
    deleteButtons[i].addEventListener('click', deleteItem);
}

let delAll = document.querySelector('.del-goods-3');
// 删除选中商品
delAll.addEventListener('click', function () {
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].checked) {
            deleteItem({ target: checkboxes[i] });
        }
    }

});

//跳转到vip首页
let homeDom = document.querySelector(' .homepage-txt');
homeDom.addEventListener('click', function () {
    location.href = "../vip.html";
});